import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

/**
 * 
 * This class is used to handle the operations required for the View Objects button.
 * 
 * @author Interactive Syllabus
 * 
 */
public class ViewObjects
{
    private Map map;
    private ArrayList<Map> mapList;
    private ArrayList<MapObject> objects;
    private MapPanel containingPanel;

    // initialize the class
    public ViewObjects(Map map, MapPanel containingPanel)
    {
	this.map = map;
	this.objects = map.getMapObjects();
	this.containingPanel = containingPanel;
	this.mapList = containingPanel.getMapList();
    }

    /**
     * This method handles displaying the selector to hide or reveal map objects.
     */
    public void displayRadioBoxSelector()
    {
	int i;
	int j;
	JFrame containingWindow = new JFrame("View Objects");
	JTabbedPane contentPanel = new JTabbedPane();
	GridLayout buttonGrid = new GridLayout(0, 1);
	// contentPanel.setLayout(buttonGrid);
	JPanel[] tabs = new JPanel[this.mapList.size()];
	ArrayList<JCheckBox[]> checkBoxList = new ArrayList<JCheckBox[]>();

	for (i = 0; i < this.mapList.size(); i++)
	{
	    this.map = this.mapList.get(i);
	    this.objects = this.map.getMapObjects();

	    tabs[i] = new JPanel();
	    tabs[i].setLayout(buttonGrid);

	    checkBoxList.add(new JCheckBox[this.objects.size()]);

	    for (j = 0; j < this.objects.size(); j++)
	    {

		checkBoxList.get(i)[j] = new JCheckBox(this.objects.get(j).getClass().getName() + ": " + this.objects.get(j).name, this.objects.get(j).visibility);

		checkBoxList.get(i)[j].addActionListener(new CheckBoxListener(i, j));

		tabs[i].add(checkBoxList.get(i)[j]);

	    }

	    contentPanel.addTab(this.map.title, tabs[i]);

	}
	/*
	 * int i; JFrame containingWindow = new JFrame("View Objects"); JPanel contentPanel = new JPanel(); GridLayout buttonGrid = new GridLayout(0, 1); contentPanel.setLayout(buttonGrid);
	 * 
	 * // JButton apply = new JButton("Apply"); // apply.addActionListener(new OkListener(containingWindow)); // contentPanel.add(apply); JButton[] copyButtons = new JButton[this.objects.size()]; JCheckBox[] checkBoxButtons = new JCheckBox[this.objects.size()]; for (i = 0; i < this.objects.size(); i++) { copyButtons[i] = new JButton("Copy"); copyButtons[i].addActionListener(new CopyListener(i)); checkBoxButtons[i] = new JCheckBox("Map " + objects.get(i).name, objects.get(i).visibility); checkBoxButtons[i].addActionListener(new CheckBoxListener(i)); contentPanel.add(checkBoxButtons[i]); //contentPanel.add(copyButtons[i]);
	 * 
	 * }
	 */
	// contentPanel.setPreferredSize(new Dimension(40, 80));
	// contentPanel.setVisible(true);

	/*
	 * int i; int j; JFrame containingWindow = new JFrame("View Objects"); JPanel contentPanel = new JPanel(); GridLayout buttonGrid = new GridLayout(0, 1); contentPanel.setLayout(buttonGrid); //JPanel[] tabs = new JPanel[this.mapList.size()]; ArrayList<JCheckBox[]> checkBoxList = new ArrayList<JCheckBox[]>();
	 * 
	 * for (i = 0; i < this.mapList.size(); i++){ this.map = this.mapList.get(i); this.objects = map.getMapObjects();
	 * 
	 * //tabs[i] = new JPanel(); System.out.println("1"); checkBoxList.add(new JCheckBox[this.objects.size()]); System.out.println("2"); for (j=0; j < this.objects.size(); j++){ checkBoxList.get(i)[j] = new JCheckBox(this.objects.get(j).name, objects.get(j).visibility); System.out.println("3"); checkBoxList.get(i)[j].addActionListener(new CheckBoxListener(j)); System.out.println("4"); //tabs[i].add(checkBoxList.get(i)[j]); System.out.println("5"); } //contentPanel.addTab("Map " + (i + 1), tabs[i]); System.out.println("6"); }
	 */

	containingWindow.add(contentPanel);
	containingWindow.pack();
	containingWindow.setLocationRelativeTo(null);
	containingWindow.setVisible(true);
    }

    protected class CheckBoxListener implements ActionListener
    {

	private int number;
	private int map;

	public CheckBoxListener(int i, int j)
	{
	    this.number = j;
	    this.map = i;
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
	    ViewObjects.this.mapList.get(this.map).getMapObjects().get(this.number).invertVisibility();

	    // ViewObjects.this.objects.get(number).color = Color.blue;
	    ViewObjects.this.containingPanel.repaint();
	}

    }

    protected class CopyListener implements ActionListener
    {

	private int number;

	public CopyListener(int i)
	{
	    this.number = i;
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{

	    ViewObjects.this.containingPanel.setSystemState(SystemState.ADDOBJECT);
	    Pin newPin = (Pin) ViewObjects.this.objects.get(this.number).copy();
	    newPin.displayCreatePrompt(false);
	    ViewObjects.this.containingPanel.addIncompleteObject(newPin);
	}

    }

    protected class OkListener implements ActionListener
    {
	private JFrame frame;

	public OkListener(JFrame frame)
	{
	    this.frame = frame;
	}

	@Override
	public void actionPerformed(ActionEvent arg0)
	{
	    this.frame.dispose();
	}
    }

}
